package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import com.itheima.pojo.User;
import com.itheima.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

/**
 * @author ：shipotian
 * @date ：Created in 2019/4/28
 * @description ：用户DAO实现类
 * @version: 1.0
 */
public class UserDaoImpl implements UserDao {

    //利用C3P0工具类获取DataSource 构建QueryRunner对象 用于还行sql语句
   private QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());

   //注册
    @Override
    public void register(User user) throws Exception {

        //sql语句
        String sql = "insert into shop_user values(?,?,?,?,?,?,?,?,?,?)";

        //sql语句参数 ?,?,?,?,?,?,?,?,?,?
        Object[] params = {user.getUid(),user.getUsername(),user.getPassword(),user.getName(),
                user.getEmail(),user.getBirthday(),user.getGender(),user.getState(),user.getCode(),user.getRemark()};

        //queryRunner执行sql语句
        queryRunner.update(sql,params);
    }

    //登陆
    @Override
    public User login(String username, String password) throws Exception {

        //sql语句
        String sql = "select * from shop_user where username=? and password=?";
        Object[] params = {username,password};

        //执行sql语句 返回user对象 new BeanHandler<>(User.class)!!!
        User user = queryRunner.query(sql, new BeanHandler<>(User.class), params);
        return user;
    }
}
